package com.lqs.blog.service.Impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lqs.blog.mapper.ArticleMapper;
import com.lqs.blog.mapper.CommentMapper;
import com.lqs.blog.pojo.Comment;
import com.lqs.blog.pojo.SysUser;
import com.lqs.blog.service.CommentService;
import com.lqs.blog.service.SysUserService;
import com.lqs.blog.service.ThreadService;
import com.lqs.blog.utils.UserThreadLocal;
import com.lqs.blog.vo.CommentVo;
import com.lqs.blog.vo.Result;
import com.lqs.blog.vo.UserVo;
import com.lqs.blog.vo.param.CommentParam;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ThreadService threadService;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Result CommentsByArticleId(Long id) {
        /*1.根据文章id查询评论列表 从comment表查找
         *2.根据作者id查询作者信息
         *3.如果level等于1要去查询是否有子评论
         *4.如果有根据评论id进行查询 （parent_id）
         */
        LambdaUpdateWrapper<Comment> wrapper = new LambdaUpdateWrapper();
        wrapper.eq(Comment::getArticleId, id);
        wrapper.eq(Comment::getLevel, 1);
        wrapper.orderByDesc(Comment::getCreateDate);

        List<Comment> commentList = commentMapper.selectList(wrapper);//得到评论列表
        List<CommentVo> commentVoList = copyList(commentList);

        return Result.success(commentVoList);
    }

    private List<CommentVo> copyList(List<Comment> commentList) {
        List<CommentVo> commentVos = new ArrayList<>();
        for (Comment comment : commentList) {
            commentVos.add(copy(comment));
        }
        return commentVos;
    }

    private CommentVo copy(Comment comment) {
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment, commentVo);
        commentVo.setId(String.valueOf(comment.getId()));
        //转换时间格式
        commentVo.setCreateDate(new DateTime(comment.getCreateDate()).toString("yyyy-MM-dd HH:mm"));
        //作者信息
        Long authorId = comment.getAuthorId();
        UserVo userVo = sysUserService.getUserVoById(authorId);
        commentVo.setAuthor(userVo);

        //子评论
        Integer level = comment.getLevel();
        if (1 == level) {//判断是否有子评论
            Long id = comment.getId();
            List<CommentVo> commentVos = findCommentsByParentId(id);
            commentVo.setChildrens(commentVos);
        }

        //to user 给谁评论
        if (level > 1) {
            Long toUid = comment.getToUid();
            UserVo user = sysUserService.getUserVoById(toUid);
            commentVo.setToUser(user);
        }
        return commentVo;
    }

    private List<CommentVo> findCommentsByParentId(Long id) {
        LambdaUpdateWrapper<Comment> lambdaUpdateWrapper = new LambdaUpdateWrapper();
        lambdaUpdateWrapper.eq(Comment::getParentId, id);
        lambdaUpdateWrapper.eq(Comment::getLevel, 2);
        return copyList(commentMapper.selectList(lambdaUpdateWrapper));
    }

    @Override
    public Result comment(CommentParam commentParam) {
//        通过局部线程获取登录对象
        SysUser user = UserThreadLocal.get();
        Comment comment = new Comment();
        comment.setContent(commentParam.getContent());
        comment.setArticleId(commentParam.getArticleId());
        comment.setAuthorId(user.getId());
        comment.setCreateDate(System.currentTimeMillis());//当前时间毫秒数

        Long parentId = commentParam.getParent();
        if (parentId == null || parentId == 0) {
            comment.setLevel(1);
        } else {
            comment.setLevel(2);
        }
        comment.setParentId(parentId == null ? 0 : parentId);
        Long toUserId = commentParam.getToUserId();
        comment.setToUid(toUserId == null ? 0 : toUserId);
        this.commentMapper.insert(comment);
        //增加一条评论数
        threadService.updateArticleCommentCounts(commentParam.getArticleId());
        return Result.success(null);
    }
}
